package com.feng.lbs;

import com.feng.location.MyLocation;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LBSActivity extends Activity 
{
	private Button locate = null;
	private TextView text = null;
	private Button showmap = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.locate = (Button)findViewById (R.id.locate);
        this.showmap = (Button)findViewById (R.id.showmap);
        this.text = (TextView)findViewById (R.id.display);
        
        this.locate.setOnClickListener(new BtnListener ());
        this.showmap.setOnClickListener(new BtnListener ());
        
    }
    
    class BtnListener implements OnClickListener
    {
		@Override
		public void onClick(View v) 
		{
			LocationManager lm = (LocationManager)getSystemService(MyLocation.SERVICE);
			MyLocation myLocat = new MyLocation (lm);
			Location location = myLocat.getLocation();
			// TODO Auto-generated method stub
			switch (v.getId())
			{
				case R.id.locate :
					setTextLocation (location);
					Thread t = new Thread (myLocat);
					t.start();
					break;
				case R.id.showmap :
					int lat = (int)(location.getLatitude() * 1E6);
					int lng = (int)(location.getLongitude() * 1E6);
					Intent intent = new Intent (LBSActivity.this, MyMapActivity.class);
					intent.putExtra("Lat", lat);
					intent.putExtra("Lng", lng);
					startActivity (intent);
					break;
				default:
					return;
			}
		}
    }
    
    private void setTextLocation (Location l)
    {
    	String text = "Location : ";
    	if (l != null)
    	{
    		text += "\nLat : " + l.getLatitude();
    		text += "\nLng : " + l.getLongitude();
    	}
    	else
    	{
    		text += "\nNo Location !!!";
    	}
    	this.text.setText(text);
    }
    
}